package com.changgou.service.goods.ExceptionHandler;

import com.changgou.common.pojo.ExceptionType;
import com.changgou.common.pojo.Result;
import com.changgou.common.pojo.StatusCode;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeoutException;

/*统一异常处理类*/
@ControllerAdvice
public class MyExceptionHandler {

    static Map<Class<?extends Exception>,String> map = new HashMap<>();

    static {
        map.put(TimeoutException.class,ExceptionType.TimeOutException);
        map.put(RuntimeException.class,ExceptionType.RunTimeException);
        map.put(IllegalArgumentException.class,ExceptionType.IllegalArgumentException);

    }


        @ExceptionHandler(value = Exception.class)
        @ResponseBody
        public Result error(Exception e){

            Class<? extends Exception> aClass = e.getClass();
            String message = map.get(aClass);
            if (null==message){
                return new Result(false, StatusCode.ERROR, "不好意思,页面走丢了。请稍后重试！");
            }
            return new Result(false, StatusCode.ERROR, message);

        }


}
